Technology · Deep dive · 21 min read

How Large Language Models Actually Work

One small trick, guessing the next word, repeated trillions of times. What that really means, and what nobody yet understands.
The Default Library · Technology 2 of 12 · Deep dive

Follows: Artificial Intelligence: Where It Came From and Where It Stands (Technology 1), with help from Markov Chains (Mathematics 15) and Vectors and Matrices (Mathematics 14).

In this deep dive
  1. The one trick: guess the next word (with a toy you can train)
  2. Inside the machine: tokens, embeddings and attention
  3. Training: from raw predictor to assistant
  4. Why they make things up, and how much they can hold in mind
  5. Reasoning models, and what nobody understands yet

Type "The capital of France is" into a chatbot, and before it says anything, something very simple happens. The model looks at every word so far and gives every possible next piece of text a probability. "Paris" gets a very high one. It picks a piece, adds it to the text, and does the whole thing again for the piece after that.

Everything a large language model (LLM) does, whether answering a question, writing code or a poem, or apologising for a mistake, comes from repeating that one step. The interesting questions are how a machine gets so good at it, and why being good at it looks so much like thinking.

This library is written with the help of an AI model (Anthropic's Claude), one of the systems this piece describes. Everything here is drawn from published research, not from the model's own account of itself. Part 5 explains why a model's account of itself can't be trusted.

Part 1 — The one trick: guess the next word

The idea is older than computers. In 1913 the Russian mathematician Andrey Markov went through a long passage of Pushkin's verse novel Eugene Onegin letter by letter, sorting each letter into vowel or consonant and counting what followed what. He showed that whether a letter is a vowel depends on the letter before it, and his method became the "Markov chain" (Mathematics 15 in this library). In 1948 Claude Shannon, the founder of information theory, built fake English by hand: he picked each word using only the frequencies of word pairs in real text. One of his samples begins:

"THE HEAD AND IN FRONTAL ATTACK ON AN ENGLISH WRITER THAT THE CHARACTER OF THIS POINT IS THEREFORE ANOTHER METHOD…" — Shannon, 1948

It's nonsense, but nonsense with an English rhythm. Shannon noticed that the samples became noticeably more English-like each time he let the next word depend on more of what came before. That is the basic principle behind a modern LLM.

You can try it below. This is a toy, and it is clearly labelled as one. It shows the idea of prediction by doing nothing but counting which words followed which in a short story (written for this page). It is not how a Transformer works inside, and Part 2 explains the difference. You can also paste in any text of your own and retrain it instantly.

Interactive · A toy next-word predictor (counting, not a neural network)

Look back
When stuck

Tap any bar to add that word yourself. The toy treats whole words and punctuation as its "tokens"; real models use word pieces (Part 2).

What the toy teaches, and where it breaks

It is only counting. Type "the" and look back one word. In the story, "the" is followed by "fox" 29 times out of 101 and by "crow" 26 times, so those are its two top guesses (about 29% and 26%). There is no understanding involved, only a table of what came next.

More context gives sharper guesses, then copying. Switch to looking back two words. The guesses get more sensible, but in this little story about three-quarters of all two-word contexts (145 of 193) were followed by only one word, so the toy mostly recites its training text. Language models face the same tension at enormous scale: they need enough context to make sense, but not so much that they just repeat what they read.

Temperature 0 gets stuck in loops. Set the temperature to 0, so the toy always takes its top choice, and press "Write 20 words" from "the fox". Looking back one word, it writes "The fox said to the fox said to the fox…" forever. Real chatbots have the same temperature setting. Low values make them predictable, higher values make them more varied and more error-prone.

"Guess anyway" is the seed of hallucination. Type "the fox sat on" and choose "Admit it". The word "on" never appears in the story, so the toy stops. Switch to "Guess anyway" and it falls back to shorter contexts and produces something fluent and confident with no basis in its training text. Part 4 explains why real models face a version of this pressure.

Why not just build a giant version of this table? Because the number of possible contexts explodes. A modern vocabulary has around 128,000 tokens, which gives over 16 billion possible two-token contexts. A real prompt might be thousands of tokens long, and almost every one is new: no table could have seen it. To predict well in situations it has never seen, a model has to generalise, treating "the crow sat" as being like "the fox sat" because crows and foxes are both animals in the story. That needs a way of representing words that captures what they mean, not just how they're spelled.

Part 2 — Inside the machine: tokens, embeddings and attention

Tokens: the pieces of text

A neural network only handles numbers, so text first has to be cut into pieces called tokens, each with an ID number. Whole words would need an impossibly large vocabulary, and single letters make texts very long. Nearly all modern models compromise with sub-word tokens. A widely used method, introduced to machine translation in 2015 by Rico Sennrich and colleagues, adapts an old compression trick called byte-pair encoding: start with single characters, then repeatedly merge the pair that appears most often until the vocabulary reaches a set size.

The result is a vocabulary of common words, common word parts and single characters. Meta's Llama 3 models (2024) use a vocabulary of 128,000 tokens and average about 3.94 characters per token on English text. A rare word might be split into several pieces, while "the" is a single token.

Tokens explain some odd behaviour. A model that might receive "strawberry" as two or three chunks never directly sees its individual letters, which helps explain why spelling and letter-counting puzzles can trip up systems that write fluent essays. Tokens also make some languages more expensive. A 2023 study by Aleksandar Petrov and colleagues found that the same text translated into different languages could need up to 15 times as many tokens. Since AI services charge by the token and context windows are measured in tokens, that difference has real costs for speakers of those languages.

Embeddings: meaning as position

Each token ID is then turned into an embedding, a long list of numbers that can be pictured as a point in a space with thousands of dimensions. In GPT-3 (2020) every token became a list of 12,288 numbers. In the largest Llama 3 model it is 16,384. Nobody chooses these numbers by hand. They start random and are adjusted during training until tokens used in similar ways end up near each other.

The famous demonstration came from Tomas Mikolov and colleagues at Google in 2013. With their "word2vec" vectors, taking the vector for King, subtracting Man and adding Woman landed nearest to Queen. Directions in the space had come to mean something, such as "male to female" or "country to capital", without anyone programming them. (The arithmetic of vectors is explained in Vectors and Matrices, Mathematics 14.)

An embedding is a word's address in a space of meaning. Training is what draws the map.

There's one crucial difference between word2vec and a modern LLM. In word2vec, "bank" has one fixed vector. Inside a Transformer, a token's vector is only the starting point. As it passes through the model's layers it is repeatedly updated using the words around it, so "bank" in "river bank" and "bank" in "bank loan" end up in very different places. The mechanism that does this updating is attention.

Attention: how words look at each other

Consider: "The trophy didn't fit in the suitcase because it was too big." What is "it"? The trophy. Now change one word: "…because it was too small." Now "it" is the suitcase. You resolved this instantly using knowledge about sizes and containers. A model has to do the same thing, and attention is how it does it.

Here is the intuition, leaving out the maths. At every layer, each token produces three things from its current vector:

The model compares one token's query with every earlier token's key. Good matches get high scores. The scores are turned into percentages that add up to 100%, and the token takes in that mix of the other tokens' values. Then every token does this at once.

Illustration · Where might "it" look? (made-up numbers)

Sentence: "The trophy didn't fit in the suitcase because it was too big." These percentages are invented to show the idea, not measured from any real model. Real attention is spread across many heads and layers at once.

Three details make this powerful:

Text→Tokens→Embeddings→Attention + feed-forward, layer after layer→Probabilities for the next token→Pick one, add it, repeat

Attention has a cost. Every token is compared with every other, so doubling the length of the text roughly quadruples that part of the work. That's a big reason long documents are expensive to process, and why researchers keep inventing shortcuts.

Compare the toy above. It looks back a fixed one or two words and matches spellings exactly. A Transformer looks back across thousands of tokens, decides which ones matter for this particular prediction, and compares meanings rather than spellings. Both are guessing the next word, but in very different ways.

Part 3 — Training: from raw predictor to assistant

Data, scale and billions of knobs

A model's parameters are all the numbers inside it: the embeddings, the query, key and value machinery, the feed-forward blocks. GPT-3 had 175 billion of them. Before training they are random, and the model produces gibberish.

Training is a loop, repeated an enormous number of times:

  1. Take a chunk of real text.
  2. At every position, have the model predict the next token.
  3. Measure how much probability it gave to the token that actually came next. Less probability means a bigger error (the "loss").
  4. Use backpropagation (Technology 1, Part 2) to work out how each parameter contributed to the error, and nudge every one slightly in the direction that reduces it.

No one tells the model grammar, facts or style. The only goal is "predict the next token better". Grammar, facts and style get learned because they help with that.

Model (year)ParametersTraining tokensNotes
GPT-3 (2020)175 billion300 billionWeb text filtered from 45 TB compressed down to 570 GB, plus books and Wikipedia
Chinchilla (2022)70 billion1.4 trillionSmaller but trained on more data, and it beat much larger models
Llama 3 405B (2024)405 billion15.6 trillion3.8 × 1025 calculations, on up to 16,000 H100 chips

To get a sense of 15.6 trillion tokens: a person reading eight hours a day, every day, at 238 words a minute (the pace this library's reading times assume) gets through about 42 million words a year. Even allowing for tokens being shorter than words, reading Llama 3's training text would take that person well over 100,000 years.

Why so much? In 2020 an OpenAI team, with Jared Kaplan as first author, found scaling laws: the model's prediction error falls smoothly and predictably as you increase parameters, data and compute, across more than seven orders of magnitude. In 2022 DeepMind's Chinchilla paper refined the recipe. For a given compute budget, parameters and data should grow together, which works out to roughly 20 training tokens per parameter for Chinchilla itself. These laws turned AI progress into something companies could plan and budget for. Epoch AI estimates that training compute for frontier models has grown about five-fold every year since 2020 (February 2026).

Scaling laws made progress predictable: spend ten times more and you could forecast roughly how much better the model would get.

Two caveats as of 2026. First, the biggest labs no longer disclose most of these numbers. Stanford's AI Index 2026 notes that frontier labs have stopped reporting parameter counts and training details, so outside estimates have to stand in. Second, high-quality human text is limited. The same report finds that synthetic (AI-generated) data "is still not replacing real data in pre-training", while careful filtering and better post-training are helping. One small open model it highlights performs comparably to a model with nearly 90 times as many parameters on several benchmarks, through careful data curation alone.

From autocomplete to assistant

A model straight out of pre-training (a base model) has learned to continue documents, not to help. Ask it "What's a good name for a cat?" and it might continue with more questions, as if it were reading a quiz. Turning it into an assistant takes further training stages:

StageWhat it learns fromWhat it changes
Pre-trainingTrillions of tokens of general textLanguage, facts, patterns of reasoning: the raw ability
Supervised fine-tuningA much smaller set of example conversations written or checked by peopleThe format and manner of a helpful answer
Learning from feedback (RLHF and relatives)People, or another AI following written principles, comparing answers and saying which is betterPreferences: more helpful, honest and harmless, more often

OpenAI's InstructGPT paper (2022) described the now-standard recipe. People ranked several answers to the same prompt. Those rankings trained a separate reward model to predict which answers people would prefer, and the language model was then trained with reinforcement learning to score highly on it. The result changed the field: people preferred the answers of a 1.3-billion-parameter InstructGPT over those of the original 175-billion-parameter GPT-3, a model more than 100 times larger.

Variants followed. In Anthropic's "Constitutional AI" (December 2022), the human rankings for harmlessness are largely replaced by an AI model judging answers against a written list of principles, which its authors called "RL from AI Feedback". In 2023 Stanford researchers introduced Direct Preference Optimization, which gets a similar effect from preference data without a separate reward model or reinforcement-learning loop. Most labs now combine several such methods. The details are often unpublished.

This stage is where a model's apparent "personality" comes from: its tone, its willingness to say "I'm not sure", what it refuses. It is also where trade-offs get set. The AI Index 2026 reports research finding that improving one responsible-AI quality, such as safety, can make another, such as accuracy, worse. Fine-tuning shapes behaviour. It doesn't add much new knowledge, and it doesn't remove the base model's underlying tendencies.

Part 4 — Why they make things up, and how much they can hold in mind

Hallucination

When a model states something false with confidence, such as a fake citation, a made-up quote or a wrong date, it is called a hallucination. The toy's "Guess anyway" button shows the basic problem: a system built to produce a likely continuation will produce one even when it has nothing solid to base it on. Nothing in the next-token step separately checks whether the result is true.

A 2025 paper by Adam Tauman Kalai and colleagues compares models to students taking an exam. If a blank answer scores zero and a guess might score a point, the best strategy is always to guess. Most AI benchmarks are scored this way, so training for high benchmark scores rewards confident guessing over admitting uncertainty. Their proposed fix is social as much as technical: change how the main benchmarks are scored so that "I don't know" stops being penalised.

Models are trained to be good test-takers, and on most tests a confident guess beats an honest "I don't know".

The problem is real and measurable. In June 2023 a US federal judge fined the lawyers in Mata v. Avianca $5,000 after they filed a brief citing court decisions that ChatGPT had invented. The AI Index 2026 reports a benchmark that, unusually, does not punish declining to answer. On it, hallucination rates across 26 leading models ranged from 22% to 94%, so models differ a lot. Common defences include letting the model search the web or a document collection and cite what it finds (called "retrieval"), training it to abstain, and, most importantly, a human checking anything that matters. (See Critical Thinking & Epistemic Hygiene, Skills for the AI Era 2.)

Knowing why hallucinations happen suggests three habits that help with any chatbot:

  1. Give it the source instead of asking it to remember. Paste the document, the policy or the data. A model summarising text that's in front of it has much less to invent than one recalling a detail from training.
  2. Ask for checkable specifics, then check them. Names, dates, quotes, citations and numbers are where invented details hide. Follow at least one link or reference all the way to the original.
  3. Make "I don't know" an acceptable answer. Say so in the prompt. It doesn't remove the guessing pressure described above, but it helps, and it reminds you that a fluent answer and a true answer are different things.

The context window

A model's context window is how much text it can take in at once: your prompt, the conversation so far, any documents, and its own reply. Everything outside the window is simply invisible to it.

Model (year)Context window
GPT-3 (2020)2,048 tokens: a few pages
Llama 3 405B (2024)128,000 tokens: roughly a full-length novel
Gemini 1.5 Pro (February 2024, preview)Up to 1 million tokens: by Google's estimate over 700,000 words, or an hour of video

Epoch AI estimates that context windows have grown about 30-fold per year since 2023. But accepting a long text is not the same as using it well. A 2023 Stanford-led study found models were best at using information at the beginning or end of a long input and noticeably worse in the middle ("lost in the middle"). The AI Index 2026 notes that the gap "between accepted and usable context length is wide".

One common misunderstanding: when a chatbot "remembers" you from an earlier conversation, it is usually because saved notes are put back into its context window. The model's parameters don't change while you talk to it. It learns nothing from you mid-conversation, unless the company later uses conversations for training, which is a policy choice you can often turn off.

Part 5 — Reasoning models, and what nobody understands yet

What "reasoning" models changed

In January 2022 a Google team, with Jason Wei as first author, showed that simply showing a model worked examples, with intermediate steps written out ("chain of thought"), sharply improved its maths and logic. With just eight such examples, a 540-billion-parameter model set a new record on a benchmark of grade-school maths word problems. Writing out steps gives the model more tokens, and so more computation, to spend on a hard problem before it commits to an answer.

The next step was to train for it. On 12 September 2024 OpenAI released a preview of o1, a model trained to work through a long chain of reasoning before answering. In January 2025 China's DeepSeek published R1 with open weights and a paper describing the method. It showed that reinforcement learning on problems with checkable answers, such as maths with known solutions and code that must pass tests, could build reasoning ability without human-written examples of reasoning. Behaviours such as checking its own work and changing strategy, the paper reports, emerged by themselves.

Two things changed. First, a model can now be made better by letting it think longer at answer time, not just by training a bigger model. Second, progress sped up. Epoch AI's capabilities index for the best models has risen about 14 points a year since reasoning models appeared in September 2024, against about 6 a year for non-reasoning models. A reasoning system reached gold-medal standard at the 2025 International Mathematical Olympiad (AI Index 2026). Its abilities are still uneven: the same report found the best model it tested read analogue clocks correctly only 50.1% of the time.

And a warning: the visible reasoning isn't necessarily the real reasoning. In a 2025 study by Anthropic researchers, reasoning models were given hidden hints and then examined to see whether their written reasoning admitted using them. When the models did use a hint, they mentioned it in at least 1% of cases but often under 20%. Reading a model's chain of thought is useful for spotting problems, the authors concluded, but it can't rule them out.

What we still don't know: looking inside

This is the strangest fact about LLMs. Engineers know exactly how to build and train them, but nobody fully understands what the trained network is doing. The parameters aren't written by anyone. They are found by training, and they don't come labelled.

The field that tries to read them is called interpretability, and it has made real progress:

The researchers themselves stress the limits. Even in their successful case studies, they write, their methods "only capture a small fraction of the mechanisms of the model".

The open questions

The toy and a real LLM, side by side
The toy aboveA real LLM
Basic jobPredict the next wordPredict the next token
HowCounts exact word sequencesBillions of learned parameters, embeddings and attention
Looks back1 or 2 wordsUp to hundreds of thousands of tokens or more
Unseen contextStuck, or falls back to shorter contextsGeneralises from similar meanings
Trained onOne short story (or your text)Trillions of tokens, then human and AI feedback
Can we see why it chose a word?Yes, the counts are shownOnly partly. That is the interpretability problem
The takeaway

A large language model predicts the next token, adds it and repeats. Text is cut into sub-word tokens, each token becomes a vector whose position encodes meaning, and layer after layer of attention lets every token pull in what it needs from the words before it. Training adjusts billions of parameters to predict trillions of tokens of real text, and fine-tuning with human or AI feedback turns that raw predictor into an assistant. The same design explains its weaknesses: it guesses confidently when it doesn't know, it can only see its context window, and its stated reasoning may not match what it actually computed. Reasoning models made thinking longer a new way to get better. Understanding what goes on inside is the field's biggest unsolved problem.

Try this: in the toy, set "Look back" to 1 word and the temperature to 0, then write from "the fox" and watch it loop. Raise the temperature to 1 and write again. Then paste a page of your own writing, a school essay or a set of messages, and see which of your favourite phrases the toy learns first.

Sources & Further Reading

Sources. Markov, "An Example of Statistical Investigation of the Text Eugene Onegin…" (1913; English translation in Science in Context 19(4), 2006). Shannon, "A Mathematical Theory of Communication" (1948), section 3. Sennrich, Haddow and Birch (arXiv 1508.07909). Grattafiori et al., "The Llama 3 Herd of Models" (arXiv 2407.21783: 128K vocabulary, 3.94 characters per token, 405B parameters, 15.6T tokens, 3.8×1025 FLOPs, up to 16K H100s, 126 layers, 128 heads, 16,384 and 53,248 dimensions). Petrov et al., NeurIPS 2023 (arXiv 2305.15425). Mikolov et al. (arXiv 1301.3781, 1310.4546). Vaswani et al. (arXiv 1706.03762). Brown et al., GPT-3 (arXiv 2005.14165: 175B parameters, 300B tokens, 96 layers and heads, 12,288 dimensions, 2,048-token context, 45 TB to 570 GB). Kaplan et al. (arXiv 2001.08361). Hoffmann et al., Chinchilla (arXiv 2203.15556). Epoch AI, "Trends in AI" (updated 5 February 2026). Stanford HAI, AI Index Report 2026 (April 2026). Ouyang et al., InstructGPT (arXiv 2203.02155). Bai et al., Constitutional AI (arXiv 2212.08073). Rafailov et al., DPO (arXiv 2305.18290). Kalai, Nachum, Vempala and Zhang, "Why Language Models Hallucinate" (arXiv 2509.04664). Mata v. Avianca, Inc., S.D.N.Y., decided 22 June 2023. Google, "Our next-generation model: Gemini 1.5" (15 February 2024). Liu et al., "Lost in the Middle" (arXiv 2307.03172). Wei et al., chain of thought (arXiv 2201.11903). OpenAI o1-preview release, 12 September 2024. DeepSeek-AI, R1 (arXiv 2501.12948). Chen et al., "Reasoning Models Don't Always Say What They Think" (arXiv 2505.05410). Elhage et al., "Toy Models of Superposition" (arXiv 2209.10652). Templeton et al., "Scaling Monosemanticity" (Anthropic, 2024). Gao et al. (arXiv 2406.04093). Lieberum et al., Gemma Scope (arXiv 2408.05147). Lindsey et al., "On the Biology of a Large Language Model" (Anthropic, 2025). Bender, Gebru et al., FAccT 2021. Schaeffer, Miranda and Koyejo (arXiv 2304.15004). The toy's percentages and the "75% of two-word contexts" figure were computed from its built-in story and checked in node. The attention bars are an illustration with invented numbers.